package com.itisyue.fun.common;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.http.HttpServletResponse;

//import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RestResult {

    public static void success(HttpServletResponse resp, Object obj) throws IOException {
        resp.setContentType("application/json; charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "200");
        jsonObject.put("data", obj);
        resp.getWriter().write(jsonObject.toJSONString());
    }

    public static void error(HttpServletResponse resp, String errMsg) throws IOException {
        resp.setContentType("application/json; charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "-1");
        jsonObject.put("data", errMsg);
        resp.getWriter().write(jsonObject.toJSONString());
    }

    public static void errorByOutputStream(HttpServletResponse resp, String errMsg) throws IOException {
        resp.setContentType("application/json; charset=utf-8");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "-1");
        jsonObject.put("data", errMsg);
        resp.getOutputStream().write(jsonObject.toJSONString().getBytes());
    }
}
